package com.wswy.wzcx.ui.main.community

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.Space
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.facebook.drawee.drawable.ProgressBarDrawable
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder
import com.facebook.drawee.view.SimpleDraweeView
import com.google.android.flexbox.FlexboxLayout
import com.stfalcon.frescoimageviewer.ImageViewer
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.Func1
import com.wswy.wzcx.utils.ImageUtils
import kotlinx.android.synthetic.main.topic_comment_item.view.*
import kotlinx.android.synthetic.main.topic_item_simple.view.*

internal class CommunityViewHolders{


    companion object {

        //贴子 添加
        const val TYPE_ADD = 1

        //无图片
        const val TYPE_TOPIC_1 = 2


        const val BASE_IMG_TYPE = 10

        //有图片
        const val TYPE_TOPIC_IMG_1 = BASE_IMG_TYPE+1
        const val TYPE_TOPIC_IMG_2 = BASE_IMG_TYPE+2
        const val TYPE_TOPIC_IMG_3 = BASE_IMG_TYPE+3
        const val TYPE_TOPIC_IMG_4 = BASE_IMG_TYPE+4
        const val TYPE_TOPIC_IMG_5 = BASE_IMG_TYPE+5
        const val TYPE_TOPIC_IMG_6 = BASE_IMG_TYPE+6
        const val TYPE_TOPIC_IMG_7 = BASE_IMG_TYPE+7
        const val TYPE_TOPIC_IMG_8 = BASE_IMG_TYPE+8
        const val TYPE_TOPIC_IMG_9 = BASE_IMG_TYPE+9



        const val TYPE_COMMENT_1 = 51


    }


    /**
     * 普通贴子
     */
    open class SimpleTopicViewHolder(itemView: View, block : ((Func1)->Unit?)?=null,replyClick:View.OnClickListener?=null): RecyclerView.ViewHolder(itemView){

        init {
            itemView.layout_interactive?.replyClickListener = replyClick
            itemView.layout_interactive?.onUserNoLoginClick = block
        }


        open fun bind(topic:TopicVO){

            if (!TextUtils.isEmpty(topic.model.topicTag)){
                itemView.tv_topic_subject.visibility = View.VISIBLE
                itemView.tv_topic_subject.text = topic.model.topicTag
            }else{
                itemView.tv_topic_subject.visibility = View.INVISIBLE
            }


            if (!TextUtils.isEmpty(topic.date)){
                itemView.tv_topic_date.visibility = View.VISIBLE
                itemView.tv_topic_date.text = topic.date
            }else{
                itemView.tv_topic_date.visibility = View.GONE
            }

            if(!TextUtils.isEmpty(topic.contentPreview)){
                itemView.tv_topic_content.visibility = View.VISIBLE
                itemView.tv_topic_content.text = topic.contentPreview
            }else{
                itemView.tv_topic_content.visibility = View.GONE
            }

            itemView.img_author_icon.setImageURI(topic.model.avatar)

            itemView.tv_topic_author.text = topic.model.userName

            itemView.layout_interactive.bindData(topic)

        }

    }


    /**
     * 带图片都帖子
     */
    class SimpleImgViewHolder(imgCount:Int,
                              itemView: View,
                              block : ((Func1)->Unit?)?=null,
                              replyClick:View.OnClickListener?=null,
                              val autoPlay:Boolean = false) : SimpleTopicViewHolder(itemView,block,replyClick), View.OnClickListener{

        private var imgUrls:Array<String>?=null

        private val imgViews = Array<SimpleDraweeView>(imgCount){
            SimpleDraweeView(itemView.context).apply {
                setTag(R.id.item_position,it)
                aspectRatio = 1f

                setOnClickListener(this@SimpleImgViewHolder)
                hierarchy?.setPlaceholderImage(R.drawable.image_default_complaint)
                hierarchy?.setFailureImage(R.drawable.image_default_complaint)
            }
        }

        init {
            itemView.findViewById<FlexboxLayout>(R.id.fbl_photo_container)?.apply {
                visibility = View.VISIBLE

                setPadding(0, SizeUtils.dp2px(8f),0,0)
            }?.run {

                when(imgCount){
                    1 -> {
                        //1张图片，直接添加
                        addView(imgViews[0], SizeUtils.dp2px(208f), SizeUtils.dp2px(208f))

                    }
                    else -> {

                        //

                        val itemLp = FlexboxLayout.LayoutParams(SizeUtils.dp2px(100f), SizeUtils.dp2px(100f))

                        itemLp.width = 0
                        itemLp.height = FlexboxLayout.LayoutParams.WRAP_CONTENT
                        itemLp.flexGrow = 1f

                        for (i in 0 until imgCount){

                            val lp = FlexboxLayout.LayoutParams(itemLp)

                            if(i >0 && i % 3 == 0){
                                lp.isWrapBefore = true
                            }

                            if(i % 3 != 0){
                                lp.leftMargin = SizeUtils.dp2px(4f)
                            }

                            if (i >= 3){
                                lp.topMargin = SizeUtils.dp2px(4f)
                            }

                            addView(imgViews[i],lp)
                        }

                        val remain = imgCount % 3

                        if (remain > 0) {
                            val lp = FlexboxLayout.LayoutParams(0, 10)
                            lp.leftMargin = SizeUtils.dp2px(4f)
                            lp.flexGrow = (3 - remain).toFloat()
                            addView(Space(context), lp)
                        }


                    }
                }

            }
        }

        override fun bind(topic: TopicVO) {
            super.bind(topic)

            var hasGif = false
            topic.model.images?.forEachIndexed { index, s ->


                if (autoPlay || !hasGif){
                    hasGif = ImageUtils.hasGif(s)
                    ImageUtils.showImage(imgViews[index],s,topic.imageSuffix)
                }else{
                    val url = if (topic.imageSuffix != null) s+topic.imageSuffix else s
                    imgViews[index].setImageURI(s,url)
                }


            }
            imgUrls = topic.model.images
        }

        override fun onClick(v: View?) {
            imgUrls?.let {

                val count = it.size



                val pos = (v?.getTag(R.id.item_position) as? Int)?:0


                val textView = TextView(v?.context).apply {
                    setTextColor(Color.WHITE)
                    textSize = 14f
                    layoutParams = RelativeLayout.LayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT).apply {

                        addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
                        addRule(RelativeLayout.CENTER_HORIZONTAL)
                        setPadding(SizeUtils.dp2px(6f),
                                SizeUtils.dp2px(3f),
                                SizeUtils.dp2px(6f),
                                SizeUtils.dp2px(3f))
                        bottomMargin = SizeUtils.dp2px(18f)
                        setBackgroundResource(R.drawable.fc_ind_bg)
                    }


                    text = " ${pos+1} / $count "

                }

                v?.context?:return
                val hierarchyBuilder = GenericDraweeHierarchyBuilder.newInstance(v.context.resources).apply {
                    progressBarImage = ProgressBarDrawable()
                }
                if (count > 1){

                    ImageViewer.Builder(v.context,it)
                            .setStartPosition(pos)
                            .setOverlayView(textView)
                            .setCustomImageRequestBuilder(ImageViewer.createImageRequestBuilder().apply {
                                isProgressiveRenderingEnabled = true
                            })
                            .setCustomDraweeHierarchyBuilder(hierarchyBuilder)
                            .setImageChangeListener {
                                textView.text = " ${it+1} / $count "
                            }.show()
                }else{
                    ImageViewer.Builder(v.context,it)
                            .setCustomImageRequestBuilder(ImageViewer.createImageRequestBuilder().apply {
                                isProgressiveRenderingEnabled = true
                            })
                            .setCustomDraweeHierarchyBuilder(hierarchyBuilder)
                            .setStartPosition(pos)
                            .show()
                }
            }

        }
    }


    /**
     * 评论
     */
    class CommentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

        init {

            itemView.layout_comment_interactive.hideReview()
        }

        fun bind(comment: CommentVO){

            itemView.tv_comment_date.text = comment.date

            itemView.tv_comment_content.text = comment.model.content

            itemView.img_comment_author_icon.setImageURI(comment.model.avatar)

            itemView.tv_comment_author.text = comment.model.userName

            itemView.layout_comment_interactive.bindData(comment)
        }


    }



    private fun chooseProcessImage(){

    }
}